/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDFinalizarAdmin.java
 *
 * Created on 12/01/2012, 11:57:34
 */
package view.admin;

import control.ChamadoControl;
import java.awt.Frame;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import model.Chamado;
import model.Pessoa;
import model.Resposta;

/**
 *
 * @author Felipe
 */
public class JDFinalizarAdmin extends JDialog {

    private ChamadoControl control;
    private Pessoa usuarioLogado;
    private Chamado chamado;
    private Resposta resposta;
    private boolean emAndamento;
    
    /** Creates new form JDFinalizarAdmin */
    public JDFinalizarAdmin(Frame parent, boolean modal, Chamado chamado, 
                        boolean emAndamento, Pessoa usuarioLogado) {
        super(parent, modal);
        control = ChamadoControl.getControl();
        this.usuarioLogado = usuarioLogado;
        this.chamado = chamado;
        this.emAndamento = emAndamento;
        initComponents();
        preencherComponentes();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlOeste = new javax.swing.JPanel();
        pnlCentro = new javax.swing.JPanel();
        pnlForm = new javax.swing.JPanel();
        lblAssunto = new javax.swing.JLabel();
        txtAssunto = new javax.swing.JTextField();
        lblMensagem = new javax.swing.JLabel();
        slpMensagem = new javax.swing.JScrollPane();
        txaMensagem = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaResposta = new javax.swing.JTextArea();
        lblResposta = new javax.swing.JLabel();
        lblId = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        lblAbertura = new javax.swing.JLabel();
        lblSolicitante = new javax.swing.JLabel();
        txtSolicitante = new javax.swing.JTextField();
        ftxtDtAbertura = new javax.swing.JFormattedTextField();
        ftxtHorarioAbertura = new javax.swing.JFormattedTextField();
        pnlCentroOeste = new javax.swing.JPanel();
        pnlCentroNorte = new javax.swing.JPanel();
        pnlCentroLeste = new javax.swing.JPanel();
        pnlCentroSul = new javax.swing.JPanel();
        pnlBotoes = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnFinalizar = new javax.swing.JButton();

        javax.swing.GroupLayout pnlOesteLayout = new javax.swing.GroupLayout(pnlOeste);
        pnlOeste.setLayout(pnlOesteLayout);
        pnlOesteLayout.setHorizontalGroup(
            pnlOesteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 514, Short.MAX_VALUE)
        );
        pnlOesteLayout.setVerticalGroup(
            pnlOesteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Finalização de chamado");

        pnlCentro.setLayout(new java.awt.BorderLayout());

        lblAssunto.setText("Assunto");

        txtAssunto.setEditable(false);

        lblMensagem.setText("Mensagem");

        txaMensagem.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        txaMensagem.setColumns(20);
        txaMensagem.setEditable(false);
        txaMensagem.setLineWrap(true);
        txaMensagem.setRows(5);
        txaMensagem.setWrapStyleWord(true);
        slpMensagem.setViewportView(txaMensagem);

        txaResposta.setColumns(20);
        txaResposta.setLineWrap(true);
        txaResposta.setRows(5);
        jScrollPane1.setViewportView(txaResposta);

        lblResposta.setText("Resposta");

        lblId.setText("Id");

        txtId.setEditable(false);

        lblAbertura.setText("Data de abertura");

        lblSolicitante.setText("Solicitante");

        txtSolicitante.setEditable(false);

        ftxtDtAbertura.setEditable(false);

        ftxtHorarioAbertura.setEditable(false);

        javax.swing.GroupLayout pnlFormLayout = new javax.swing.GroupLayout(pnlForm);
        pnlForm.setLayout(pnlFormLayout);
        pnlFormLayout.setHorizontalGroup(
            pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFormLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblId, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSolicitante, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblAssunto, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblMensagem, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblResposta, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(slpMensagem)
                    .addComponent(txtAssunto)
                    .addComponent(txtSolicitante)
                    .addGroup(pnlFormLayout.createSequentialGroup()
                        .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addComponent(lblAbertura)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftxtDtAbertura, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ftxtHorarioAbertura, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        pnlFormLayout.setVerticalGroup(
            pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlFormLayout.createSequentialGroup()
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblId)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAbertura)
                    .addComponent(ftxtDtAbertura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ftxtHorarioAbertura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSolicitante)
                    .addComponent(txtSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblAssunto)
                    .addComponent(txtAssunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblMensagem)
                    .addComponent(slpMensagem, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblResposta)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(118, 118, 118))
        );

        pnlCentro.add(pnlForm, java.awt.BorderLayout.CENTER);
        pnlCentro.add(pnlCentroOeste, java.awt.BorderLayout.WEST);
        pnlCentro.add(pnlCentroNorte, java.awt.BorderLayout.NORTH);
        pnlCentro.add(pnlCentroLeste, java.awt.BorderLayout.EAST);
        pnlCentro.add(pnlCentroSul, java.awt.BorderLayout.SOUTH);

        getContentPane().add(pnlCentro, java.awt.BorderLayout.CENTER);

        pnlBotoes.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlBotoes.setPreferredSize(new java.awt.Dimension(558, 50));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnFinalizar.setText("Encerrar chamado");
        btnFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinalizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlBotoesLayout = new javax.swing.GroupLayout(pnlBotoes);
        pnlBotoes.setLayout(pnlBotoesLayout);
        pnlBotoesLayout.setHorizontalGroup(
            pnlBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlBotoesLayout.createSequentialGroup()
                .addContainerGap(186, Short.MAX_VALUE)
                .addComponent(btnFinalizar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlBotoesLayout.setVerticalGroup(
            pnlBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBotoesLayout.createSequentialGroup()
                .addGroup(pnlBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFinalizar)
                    .addComponent(btnCancelar))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        getContentPane().add(pnlBotoes, java.awt.BorderLayout.PAGE_END);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-522)/2, (screenSize.height-504)/2, 522, 504);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();     
   }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinalizarActionPerformed
        if (emAndamento == true) { 
            encerrarChamadoEA(); 
        } else { 
            encerrarChamadoNAtri();
        }
   }//GEN-LAST:event_btnFinalizarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnFinalizar;
    private javax.swing.JFormattedTextField ftxtDtAbertura;
    private javax.swing.JFormattedTextField ftxtHorarioAbertura;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAbertura;
    private javax.swing.JLabel lblAssunto;
    private javax.swing.JLabel lblId;
    private javax.swing.JLabel lblMensagem;
    private javax.swing.JLabel lblResposta;
    private javax.swing.JLabel lblSolicitante;
    private javax.swing.JPanel pnlBotoes;
    private javax.swing.JPanel pnlCentro;
    private javax.swing.JPanel pnlCentroLeste;
    private javax.swing.JPanel pnlCentroNorte;
    private javax.swing.JPanel pnlCentroOeste;
    private javax.swing.JPanel pnlCentroSul;
    private javax.swing.JPanel pnlForm;
    private javax.swing.JPanel pnlOeste;
    private javax.swing.JScrollPane slpMensagem;
    private javax.swing.JTextArea txaMensagem;
    private javax.swing.JTextArea txaResposta;
    private javax.swing.JTextField txtAssunto;
    private javax.swing.JTextField txtId;
    private javax.swing.JTextField txtSolicitante;
    // End of variables declaration//GEN-END:variables

    private void encerrarChamadoNAtri() {
        try {
            String descricao = txaResposta.getText();
            Calendar dtAtual = Calendar.getInstance();
            Resposta resposta = new Resposta(dtAtual, dtAtual, descricao, usuarioLogado);
                     
            control.encerrarChamadoNAtri(this.chamado, resposta);
            
            JOptionPane.showMessageDialog(null, "Chamado encerrado com sucesso!", "Finalizar chamado", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Um erro ocorreu! \n" + ex.getMessage());
            Logger.getLogger(JDFinalizarAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void encerrarChamadoEA() {
        try {
            String descricao = txaResposta.getText();
            Calendar dtAtual = Calendar.getInstance();
            Resposta resposta = new Resposta(dtAtual, dtAtual, descricao, usuarioLogado);
                     
            control.encerrarChamadoEA(this.chamado, resposta);
            
            JOptionPane.showMessageDialog(null, "Chamado encerrado com sucesso!", "Finalizar chamado", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Um erro ocorreu! \n" + ex.getMessage());
            Logger.getLogger(JDFinalizarAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    private void preencherComponentes() {
        SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
                        
        txtId.setText(Long.toString(this.chamado.getId()));
        ftxtDtAbertura.setText(sdf1.format(chamado.getDtAbertura().getTime()));
        ftxtHorarioAbertura.setText(sdf2.format(chamado.getDtAbertura().getTime()));
        txtSolicitante.setText(chamado.getSolicitante().getNome());
        
        txtAssunto.setText(this.chamado.getTitulo());
        txaMensagem.setText(chamado.getDescricao());
    }
    
}